﻿package core
{
	public class Model
	{
		public function Model()
		{
		}
		
		private static var _instance:Model;
		
		public static function get instance():Model {
			if(_instance == null){
				_instance = new Model();
			}
			return _instance;
		}
		
		public var points:Number = 100;// 点数
		
		public var hintMode:int = 1;// 提示类型
		
		public var insureMode:int = 0;// 如果是-1,就是从来不要,如果是1就要,如果是2不要,如果是3,这局不
		
		public var players:Array = [];
		
		private var copyPlayers:Array = [];
		
		private var cards:Array = null;
		
		public var take:Number = 0;
		
		public function createCards():void {
			cards = [];
			for(var i:int = 0; i < Config.MAX_COLOR_LENGTH; i++){
				for(var j:int = 1; j <= Config.MAX_CARD_LENGTH; j++){
					var cardItemObject:CardItemObject = new CardItemObject(j,i);
					cards.push(cardItemObject);
				}
			}
		}
		
		private var test:int = 0;
		public function random():CardItemObject {
			var index:int = Math.floor(this.cards.length * Math.random());
			//index = 0;
			
			/*if(test == 1){
				index = 0;
			}else if(test == 3){
				index = 12;
			}
			test++;*/
			
			//index = 5;
			//index = 5;
			var cardItemObject:CardItemObject = this.cards[index];
			
			this.cards.splice(index,1);
			return cardItemObject;
		}
		
		public function copy(sortabled:Boolean = true):void {
			this.players = this.copyPlayers.concat();
			if(sortabled){
				this.sort();
			}
		}
		
		public function slice():void {
			this.copy();
			for(var i:int = 0; i < this.players.length; i++){
				var owner:int = this.players[i];
				if(owner > Config.MAX_USERS){
					this.players.splice(i,1);
					i--;
				}
			}
		}
		
		/**
		 * 插入数据时需要注意几个问题
		 * 先要把原来players的数据取出来,因为可能会执行到了一半.
		 * 并且先把原始的copy的数据复制过来
		 * 然后把一个数组分成2部分头和尾 
		 * @param value
		 * 
		 */		
		public function insert(value:int):void {
			var owner:int = value - 6;
			var temp:Array = [];// 取出这个数据,因为有一些原有的数据已经没有了
			// 获取已经剔除的数据
			var i:int = 0;
			var o:int;
			for(; i < this.copyPlayers.length; i++){
				o = this.copyPlayers[i];
				if(players.indexOf(o) == -1){
					temp.push(o);
				}
			}
			this.copy(false);// 先copy原来的数据,防止出错
			var index:int = this.players.indexOf(owner);
			var first:Array = this.players.slice(0,index + 1);
			var tail:Array = this.players.slice(index + 1,this.players.length);
			first.push(value);
			first = first.concat(tail);
			this.players = first.concat();
			this.copyPlayers = this.players.concat();
			i = 0;
			for(; i < temp.length; i++){
				o = temp[i];
				var indexOf:int = this.players.indexOf(o);
				if(indexOf != -1){
					this.players.splice(indexOf,1);
				}
			}
		}
		
		public function addArray(value:Array):void {
			this.players = this.players.concat(value);
		}
		
		public function conact():void {
			this.copyPlayers = this.players.concat();
		}
		
		public function splice(owner:int):void {
			var index:int = this.players.indexOf(owner);
			this.players.splice(index,1);
		}
		
		public function shift():* {
			return this.players.shift();
		}
		
		/**
		 * 添加用户 
		 * @param value
		 * 
		 */		
		public function addOwner(value:int):void {
			if(this.players.indexOf(value) == -1){
				this.players.push(value);
			}
		}
		
		public function get has():Boolean {
			return this.players.length > 0;
		}
		
		public function get first():int {
			return this.players[0];
		}
		
		public function sortFunc(a:int,b:int):int {
			if(a > b){
				return 1;
			}
			if(a < b){
				return -1;
			}
			return 0;
		}
		
		public function sort():void {
			this.players.sort(sortFunc);
		}
		
		public function update(value:Number):void {
			this.points += value;
		}
	}
}